package com.ruoyi.web.controller.assistant;

import com.ruoyi.assistant.domain.Course;
import com.ruoyi.assistant.domain.Exam;
import com.ruoyi.assistant.domain.Question;
import com.ruoyi.assistant.dto.ExamAnswerDto;
import com.ruoyi.assistant.dto.ExamDto;
import com.ruoyi.assistant.service.ICourseService;
import com.ruoyi.assistant.service.IExamService;
import com.ruoyi.assistant.service.IQuestionService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@RestController
@RequestMapping("/assistant/question")
public class AssistantController {

    @Autowired
    private IQuestionService questionService;

    /**
     * 模板下载
     * @return
     */
    @ResponseBody
    @GetMapping("/importTemplate")
    @RequiresPermissions("assistant:question:view")
    public AjaxResult dowloadTemplate()
    {
        ExcelUtil<ExamAnswerDto> util = new ExcelUtil<>(ExamAnswerDto.class);
        return util.importTemplateExcel("问题答案关联模板", "导入数据模板");
    }

    @ResponseBody
    @PostMapping("/importData")
    @RequiresPermissions("assistant:question:import")
    public AjaxResult importData(@RequestBody MultipartFile file)
    {
      ExcelUtil<ExamAnswerDto> excelUtil = new ExcelUtil<>(ExamAnswerDto.class);
        try {
            List<ExamAnswerDto> examAnswerDtos = excelUtil.importExcel(file.getInputStream(), 1);
            return questionService.matchingAnswers(examAnswerDtos);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /*@CrossOrigin
    @PostMapping("/importAnswer")
    public AjaxResult importAnswer(@RequestBody MultipartFile file) {

        return questionService.fillQuestions(examDto);
    }*/
    @CrossOrigin
    @PostMapping("/save")
    public AjaxResult saveQuestions(@RequestBody ExamDto examDto) {
       return questionService.saveQuestions(examDto);
    }

    @CrossOrigin
    @PostMapping("/fillAnswer")
    public AjaxResult fillQuestions(@RequestBody ExamDto examDto) {
        return questionService.fillQuestions(examDto);
    }
}

